package com.appkubes.mall.admin.application.plugin.plugin.service.impl;

import com.appkubes.common.exception.BusinessException;
import com.appkubes.mall.admin.application.plugin.plugin.dao.PluginDao;
import com.appkubes.mall.admin.application.plugin.plugin.entity.constant.PluginCodeConstant;
import com.appkubes.mall.admin.application.plugin.plugin.entity.vo.PluginAddVO;
import com.appkubes.mall.admin.application.plugin.plugin.service.PluginService;
import com.appkubes.mall.core.entity.MallPlugin;
import com.appkubes.mall.core.service.impl.BaseMallPluginServiceImpl;
import lombok.Data;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;

/**
 * @Author: Rongjie
 * @Date: 2020/1/15 23:08
 * @Description:
 */
@Service
@Data
public class PluginServiceImpl extends BaseMallPluginServiceImpl implements PluginService {
    @Resource
    PluginDao pluginDao;

    @Override
    @Transactional
    public MallPlugin addPlugin(PluginAddVO addVO) throws BusinessException {
        MallPlugin query = new MallPlugin();
        query.setSystemPluginCode(addVO.getSystemPluginCode());
        List<MallPlugin> plugins = this.selectListByEntity(query);
        if (plugins.size() > 0) {
            throw new BusinessException("已开通插件");
        }
        MallPlugin plugin = new MallPlugin();
        BeanUtils.copyProperties(addVO, plugin);
        plugin.setAppKey(null);
        this.save(plugin);
        return plugin;
    }

    @Override
    public MallPlugin getPlugin(String appKey, String systemPluginCode) {
        MallPlugin query = new MallPlugin();
        query.setSystemPluginCode(systemPluginCode);
        List<MallPlugin> plugins = this.selectListByEntity(query);
        if (plugins.size() > 0) {
            return plugins.get(0);
        }
        return null;
    }

    @Override
    public MallPlugin getPlugin(String appKey, PluginCodeConstant systemPluginCode) {
        return getPlugin(appKey, systemPluginCode.name());
    }
}
